<%@ page import="com.casework.auth.Company" %>
<!doctype html>
<html>
<head>
    <meta name="layout" content="main">
    <g:set var="entityName" value="${message(code: 'company.label', default: 'Company')}"/>
    <title><g:message code="default.show.label" args="[entityName]"/></title>
</head>

<body>
<a href="#show-company" class="skip" tabindex="-1"><g:message code="default.link.skip.label"
                                                              default="Skip to content&hellip;"/></a>

<div class="nav" role="navigation">
    <ul>
        <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
        <li><g:link class="list" action="list"><g:message code="default.list.label" args="[entityName]"/></g:link></li>
        <li><g:link class="create" action="create"><g:message code="default.new.label"
                                                              args="[entityName]"/></g:link></li>
    </ul>
</div>

<div id="show-company" class="content scaffold-show" role="main">
    <h1><g:message code="default.show.label" args="[entityName]"/></h1>
    <g:if test="${flash.message}">
        <div class="message" role="status">${flash.message}</div>
    </g:if>
    <g:if test="${flash.email}">
        <div class="email-message">
            ${flash.email}
        </div>
    </g:if>
    <ol class="property-list company">

        <g:if test="${companyInstance?.name}">
            <li class="fieldcontain">
                <span id="name-label" class="property-label"><g:message code="company.name.label"
                                                                        default="Name"/></span>

                <span class="property-value" aria-labelledby="name-label"><g:fieldValue bean="${companyInstance}"
                                                                                        field="name"/></span>

            </li>
        </g:if>

        <g:if test="${companyInstance?.contact}">
            <li class="fieldcontain">
                <span id="contact-label" class="property-label"><g:message code="company.contact.label"
                                                                           default="Contact"/></span>

                <span class="property-value" aria-labelledby="contact-label"><g:fieldValue bean="${companyInstance}"
                                                                                           field="contact"/></span>

            </li>
        </g:if>

        <g:if test="${companyInstance?.phoneNumber}">
            <li class="fieldcontain">
                <span id="phoneNumber-label" class="property-label"><g:message code="company.phoneNumber.label"
                                                                               default="Phone Number"/></span>

                <span class="property-value" aria-labelledby="phoneNumber-label"><g:fieldValue bean="${companyInstance}"
                                                                                               field="phoneNumber"/></span>
            </li>
        </g:if>

        <g:if test="${companyInstance?.numberOfLicenses}">
            <li class="fieldcontain">
                <span id="numberOfLicenses-label" class="property-label"><g:message code="company.numberOfLicenses.label"
                                                                               default="Number Of Licenses"/></span>

                <span class="property-value" aria-labelledby="numberOfLicenses-label"><g:fieldValue bean="${companyInstance}"
                                                                                               field="numberOfLicenses"/></span>
            </li>
        </g:if>

        <g:if test="${companyInstance?.email}">
            <li class="fieldcontain">
                <span id="email-label" class="property-label"><g:message code="company.email.label"
                                                                         default="Email"/></span>

                <span class="property-value" aria-labelledby="email-label"><g:fieldValue bean="${companyInstance}"
                                                                                         field="email"/></span>

            </li>
        </g:if>

        <g:if test="${companyInstance?.expiryDate}">
            <li class="fieldcontain">
                <span id="expiryDate-label" class="property-label"><g:message code="company.expiryDate.label"
                                                                              default="Expiry Date"/></span>

                <span class="property-value" aria-labelledby="expiryDate-label"><g:fieldValue bean="${companyInstance}"
                                                                                              field="expiryDate"/></span>

            </li>
        </g:if>

        <g:if test="${companyInstance?.employees}">
            <li class="fieldcontain">
                <span id="employees-label" class="property-label"><g:message code="company.employees.label"
                                                                             default="Employees"/></span>

                <g:each in="${companyInstance.employees}" var="o">
                    <span class="property-value" aria-labelledby="offers-label">
                        <g:link controller="authUser" action="edit" id="${o.id}">${o?.encodeAsHTML()}</g:link>
                        <g:link controller="authUser" action="changePassword" id="${o.id}"  class="change-password">(ChangePassword)</g:link>
                        <a href="#" class="close-button" data-name="User" data-id="${o.id}">x</a>
                    </span>
                </g:each>

            </li>
        </g:if>
        <li class="fieldcontain"><span class="property-label"></span>
            <g:if test="${!companyInstance.employees}">
                <span id="employees-label" class="property-label"><g:message code="company.employees.label"
                                                                             default="Employees"/></span>
            </g:if>
            <span class="property-value" aria-labelledby="measures-label">
                <g:link action="create" class='add-employee' controller="authUser"
                        params="[companyId:companyInstance.id]">Add Employee</g:link>
            </span>
        </li>

    </ol>
    <g:form>
        <fieldset class="buttons">
            <g:hiddenField name="id" value="${companyInstance?.id}"/>
            <g:link class="edit" action="edit" id="${companyInstance?.id}"><g:message code="default.button.edit.label"
                                                                                      default="Edit"/></g:link>
            <g:actionSubmit class="delete" action="delete"
                            value="${message(code: 'default.button.delete.label', default: 'Delete')}"
                            onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');"/>
        </fieldset>
    </g:form>
</div>

<r:script>
    $('.close-button').css('visibility', 'visible');
    $('.close-button').click(function(e){

        var id = $(e.target).data('id');
        var element = e.target
        $.ajax({
            type: 'POST',
            url: "${request.getContextPath()}/company/deleteUser",
            data: {'id': id, 'companyId': "${companyInstance.id}"},
            success: function(msg){

             if(msg.result == 'failure'){
                alert('You cannot delete yourself');
             } else{
                $(element).parent('.property-value').remove();
                $(element).parent('.description').remove();
            }
            },
            error: function(e, xhr, status){
                alert('Error connecting to server');
            }
         })
         return false;
    });

</r:script>
</body>
</html>
